package com.llyb.controller;

import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesis;
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesisParam;
import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesisResult;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.utils.JsonUtils;
import dev.langchain4j.community.model.dashscope.WanxImageModel;
import dev.langchain4j.data.image.Image;
import dev.langchain4j.data.message.ImageContent;
import dev.langchain4j.data.message.TextContent;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.response.ChatResponse;
import dev.langchain4j.model.output.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Base64;

@RestController
@Slf4j
public class ImageModelController {

    @Autowired
    private ChatModel chatModel;

    @Autowired
    private WanxImageModel wanxImageModel;

    @Value("classpath:static.images/鼠.jpg")
    private Resource resource;

    // http://localhost:9006/image/call
    @GetMapping("/image/call")
    public String readImageProcessor() throws IOException {

        byte[] imageByte = resource.getContentAsByteArray();
        String base64String = Base64.getEncoder().encodeToString(imageByte);

        UserMessage userMessage = UserMessage.from(
                TextContent.from("分析一下图片"),
                ImageContent.from(base64String, "image/jpg")
        );


        ChatResponse chatResponse = chatModel.chat(userMessage);
        String text = chatResponse.aiMessage().text();
        System.out.println(text);
        return text;
    }

    @GetMapping("/image/create1")
    public String createImageProcessor() throws IOException {
        Response<Image> imageResponse = wanxImageModel.generate("美女");
        System.out.println(imageResponse.content().url());

        return imageResponse.content().url().toString();
    }

    @GetMapping("/image/create2")
    public String createImageAdvance()  {
        String prompt = "好看的风景";
        ImageSynthesisParam param = ImageSynthesisParam.builder().
                apiKey(System.getenv("aliQwen-api")).
                model(ImageSynthesis.Models.WANX_V1).
                prompt(prompt).
                style("<watercolor>").
                n(1).
                size("1024*1024").
                build();

        ImageSynthesis imageSynthesis = new ImageSynthesis();
        ImageSynthesisResult result = null;

        try {
            result = imageSynthesis.call(param);
        } catch (NoApiKeyException e) {
            e.printStackTrace();
        }

        System.out.println(JsonUtils.toJson(result));
        return JsonUtils.toJson(result);
    }

}
